// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Oil Fall Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.startPos.y
private alias object.value1 : object.endPos.y


// ========================
// Events
// ========================

event ObjectDraw
	if object.startPos.y == object.endPos.y
		DrawSprite(object.frame)
	else
		temp0 = object.startPos.y
		temp0 += 0x100000
		while temp0 < object.endPos.y
			DrawSpriteXY(object.frame, object.xpos, temp0)
			temp0 += 0x200000
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-2, -16, 6, 32, 182, 51)
	SpriteFrame(-2, -16, 6, 32, 182, 148)
	SpriteFrame(-2, -16, 6, 32, 182, 59)
	SpriteFrame(-2, -16, 6, 32, 182, 156)
	SpriteFrame(-8, -16, 16, 32, 189, 75)
	SpriteFrame(-24, -16, 48, 32, 189, 107)

	foreach (TypeName[Oil Fall], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.y = object[arrayPos0].propertyValue
		object[arrayPos0].startPos.y &= 7
		object[arrayPos0].startPos.y <<= 20
		object[arrayPos0].endPos.y = object[arrayPos0].startPos.y
		FlipSign(object[arrayPos0].startPos.y)
		object[arrayPos0].startPos.y += object[arrayPos0].ypos
		object[arrayPos0].endPos.y += object[arrayPos0].ypos

		if object[arrayPos0].propertyValue > 0
			object[arrayPos0].frame = object[arrayPos0].propertyValue
			object[arrayPos0].frame >>= 3
			object[arrayPos0].frame += 2
		else
			object[arrayPos0].frame = object[arrayPos0].propertyValue
			object[arrayPos0].frame >>= 3
			object[arrayPos0].frame &= 1
		end if

		if object[arrayPos0].frame != 4
			object[arrayPos0].drawOrder = 4
		end if
	next
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // size
			checkResult = object.propertyValue
			checkResult &= 7
			break
			
		case 1 // frameID
			checkResult = object.propertyValue
			checkResult >>= 3
			checkResult &= 3
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // size
			temp0 = 7
			Not(temp0)
			object.propertyValue &= temp0
			editor.variableValue &= 7

			object.propertyValue |= editor.variableValue
			break
			
		case 1 // frameID
			temp0 = 3
			temp0 <<= 3
			Not(temp0)
			object.propertyValue &= temp0
			editor.variableValue &= 3
			editor.variableValue <<= 3

			object.propertyValue |= editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.startPos.y = object.propertyValue
	object.startPos.y &= 7
	object.startPos.y <<= 20
	object.endPos.y = object.startPos.y
	FlipSign(object.startPos.y)
	object.startPos.y += object.ypos
	object.endPos.y += object.ypos

	if object.propertyValue > 0
		object.frame = object.propertyValue
		object.frame >>= 3
		object.frame += 2
	else
		object.frame = object.propertyValue
		object.frame >>= 3
		object.frame &= 1
	end if

	if object.startPos.y == object.endPos.y
		DrawSprite(object.frame)
	else
		temp0 = object.startPos.y
		temp0 += 0x100000
		while temp0 < object.endPos.y
			DrawSpriteXY(object.frame, object.xpos, temp0)
			temp0 += 0x200000
		loop
	end if
end event


event RSDKLoad
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-2, -16, 6, 32, 182, 51)
	SpriteFrame(-2, -16, 6, 32, 182, 148)
	SpriteFrame(-2, -16, 6, 32, 182, 59)
	SpriteFrame(-2, -16, 6, 32, 182, 156)
	SpriteFrame(-8, -16, 16, 32, 189, 75)
	SpriteFrame(-24, -16, 48, 32, 189, 107)

	AddEditorVariable("size")
	SetActiveVariable("size")

	AddEditorVariable("frameID")
	SetActiveVariable("frameID")
end event
